//
//  BulletManager.m
//  Project 5
//
//  Created by vbnn2 on 6/21/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "BulletManager.h"
#import "MainGameScene.h"

@implementation BulletManager

-(id) init
{
    if (self = [super init])
    {
        mBatchNode = [CCSpriteBatchNode batchNodeWithFile:BATCH_CIRCLE_SEGMENT_PNG];
        [self addChild:mBatchNode];
        
        mListBulletActive = [[NSMutableArray alloc] init];
        mListBulletInactive = [[NSMutableArray alloc] init];
        [self prepareBullet];
    }
    
    return self;
}

-(void) prepareBullet
{
    int count = MAX_BULLET - mListBulletInactive.count;
    for (int i = 0; i < count; i++)
    {
        Bullet *bullet = [[Bullet alloc] initWithType:TYPE_NORMAL color:[self randomBulletColor]];
        [mListBulletInactive addObject:bullet];
        [mBatchNode addChild:bullet];
        
        [bullet release];
    }
    
    [self arrangeBullet];
}

-(void) arrangeBullet
{
    if (mListBulletInactive.count > 0)
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        Bullet *bullet;
        bullet = [self getCurrentBullet];
        [bullet setPosition:ccp(0, 0)];
        
        for (int i = 1; i < mListBulletInactive.count; i++)
        {
            bullet = (Bullet *)[mListBulletInactive objectAtIndex:i];
            [bullet setPosition:ccp(-winSize.width / 2 - 50 * i + 200, -winSize.height / 2 + 100)];
            [bullet setVisible:(i < 3 ? YES : NO)];
        }
        
        bullet = (Bullet *)[mListBulletInactive objectAtIndex:2];
        [bullet setType:TYPE_NORMAL color:[self randomBulletColor]];
    }
}

-(COLOR_TYPE) randomBulletColor
{
    MainGameScene *mainGame = [MainGameScene getInstance];
    CircleOrbitManager *circleOrbitManager = [mainGame getCircleOrbitManager];
    NSArray *listAvailableColor = [circleOrbitManager getListAvailableColor];
    NSMutableArray *listRand = [NSMutableArray array];
    CCLOG(@"++++ %d", [circleOrbitManager getListAvailableColor].count);
    
    int range = 0;
    int index;
    for (int i = 0; i < listAvailableColor.count; i++)
    {
        index = [[listAvailableColor objectAtIndex:i] intValue];
        range += [[[LevelManager shareLevelManager]._listBulletRate objectAtIndex:index] intValue];
        [listRand addObject:[NSNumber numberWithInt:range]];
        
        CCLOG(@"--- index: %d", index);
        CCLOG(@"--- range: %d", range);
    }
    
    if (range == 0) return index;
    int randomRange = rand() % range + 1;
    for (int i = 0; i < listRand.count; i++)
    {
        int r = [[listRand objectAtIndex:i] intValue];
        if (randomRange <= r)
            return (COLOR_TYPE)([[listAvailableColor objectAtIndex:i] intValue]);
    }
    return 0;
}

-(void) processTouch:(UITouchPhase)touchPhase position:(CGPoint)position
{
    CGPoint vDir = ccpSub(position, self.position);
    vDir = ccpNormalize(vDir);
    
    if (touchPhase == UITouchPhaseBegan)
    {
        
    }
    else if (touchPhase == UITouchPhaseMoved)
    {
        
    }
    else if (touchPhase == UITouchPhaseEnded)
    {
        [self fireBulletWithDirection:vDir];
    }
}

-(void) fireBulletWithDirection:(CGPoint)direction
{
    Bullet *bullet = [self getCurrentBullet];
    [bullet fireWithDirection:direction];
    [mListBulletInactive removeObject:bullet];
    [mListBulletActive addObject:bullet];
    
    [self prepareBullet];
    
    CCLOG(@"active = %d, inactive = %d", mListBulletActive.count, mListBulletInactive.count);
}

-(void) startUpdate
{
    [self scheduleUpdate];
}

-(void) stopUpdate
{
    [self unscheduleUpdate];
}

-(void) update:(ccTime)delta
{
    for (int i = 0; i < mListBulletActive.count; i++)
    {
        Bullet *b = [mListBulletActive objectAtIndex:i];
        if ([b isDestroy])
        {
            [mListBulletActive removeObject:b];
            [mListBulletInactive addObject:b];
        }
    }
}

-(Bullet *) getCurrentBullet
{
    Bullet *bullet;
    bullet = (Bullet *)[mListBulletInactive objectAtIndex:0];
    return bullet;
}

-(void) dealloc
{
    [super dealloc];
    
    [mListBulletActive removeAllObjects];
    [mListBulletActive release];
    mListBulletActive = nil;
    
    [mListBulletInactive removeAllObjects];
    [mListBulletInactive release];
    mListBulletInactive = nil;
}

@end
